package com.solcoo.customer.ui;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;

import com.android.common.util.DialogUtil;
import com.solcoo.customer.ApplicationExt;
import com.solcoo.customer.R;
import com.solcoo.customer.base.BaseActivity;
import com.solcoo.customer.model.Version;
import com.solcoo.customer.ui.card.CardActivity;
import com.solcoo.customer.ui.home.HomeActivity;
import com.solcoo.customer.ui.setting.SettingActivity;

public abstract class ToolBarActivity extends BaseActivity {

	private Dialog outDialog;// 退出确认

	public static final Class<? extends ToolBarActivity> HOME = HomeActivity.class;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		outDialog = DialogUtil.createDialog(this, onConfirmQuitClickListener,
				DialogUtil.onNegativeListener, null, "确定退出系统？", null);
	}

	@Override
	protected void onResume() {
		super.onResume();
		Version.showDialog();
		ImageButton currentTab = (ImageButton) findViewById(getCurId());
		if (currentTab != null) {
			currentTab.setBackgroundResource(R.drawable.toolbar_bg_p);
			switch (getCurId()) {
			case R.id.home_btn:
				currentTab.setImageResource(R.drawable.home_icon_p);
				break;
			case R.id.card_btn:
				currentTab.setImageResource(R.drawable.card_icon_p);
				break;
			// case R.id.follow_btn:
			// currentTab.setImageResource(R.drawable.qrcode_icon_p);
			// break;
			// case R.id.find_btn:
			// currentTab.setImageResource(R.drawable.find_icon_p);
			// break;
			case R.id.setting_btn:
				currentTab.setImageResource(R.drawable.setting_icon_p);
				break;
			default:
				currentTab.setImageResource(R.drawable.home_icon_p);
				break;
			}
		}
		View back = findViewById(R.id.btn_go_back);
		if (back != null) {
			back.setVisibility(View.INVISIBLE);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			outDialog.show();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	public void tabOnClick(View v) {
		if (v.getId() == getCurId()) {
			return;
		}
		Intent it = new Intent();
		it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		switch (v.getId()) {
		case R.id.home_btn:
			it.setClass(ToolBarActivity.this, HomeActivity.class);
			break;
		case R.id.card_btn:
			it.setClass(ToolBarActivity.this, CardActivity.class);
			break;
		// case R.id.follow_btn:
		// it.setClass(ToolBarActivity.this, FollowActivity.class);
		// break;
		// case R.id.find_btn:
		// it.setClass(ToolBarActivity.this, FindActivity.class);
		// break;
		case R.id.setting_btn:
			it.setClass(ToolBarActivity.this, SettingActivity.class);
			break;
		default:
			it.setClass(ToolBarActivity.this, ToolBarActivity.HOME);
			break;
		}
		startActivity(it);
	}

	public static DialogInterface.OnClickListener onConfirmQuitClickListener = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			dialog.dismiss();
			ApplicationExt.destory();
		}
	};

	protected abstract int getCurId();
}
